﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using CodeStash.Common.DataAccess.EntityFramework;
using CodeStash.Common.DataAccess.Repository;
using CodeStash.Models.Security;
using CodeStash.Models.Settings;
using CodeStash.Models.Snippet;
using System.Web;

namespace CodeStash.Utils
{
    public static class CodeSnippetUtils
    {


        public static string GetUsersSavedHighlightingCSS(IIdentity Identity)
        {
            UserSettingsProfileModel profile = UserSettingsProfileModel.GetUserProfile(Identity.Name);
            List<HighlightingCSS> highlightingCSSList = ChangeSettingsModel.CompleteHighlightingCSSList.Value;
            string highlightingCSSName = highlightingCSSList.Where(x => x.Id == 23).Single().CSSName;
            if (highlightingCSSList.Where(x => x.Id == profile.HighlightingCSSId).Any())
            {
                highlightingCSSName = highlightingCSSList.Where(x => x.Id == profile.HighlightingCSSId).Single().CSSName;
            }
            return highlightingCSSName;
        }


        public static string ReadContentFromWebUrl(string url)
        {
            try
            {
                System.Net.HttpWebRequest fr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(url));
                if ((fr.GetResponse().ContentLength > 0))
                {
                    System.IO.StreamReader str = new System.IO.StreamReader(fr.GetResponse().GetResponseStream());
                    return str.ReadToEnd();
                }
                return "";
            }
            catch 
            {
                return "";
            } 
        }


        public static List<CodeSnippet> AddLanguageCategoryLookups(
            List<CodeSnippet> codeSnippets,
            IRepository<Language> languageRepository,
            IRepository<CodeCategory> categoryRepository)
        {
            foreach (CodeSnippet snippet in codeSnippets)
            {
                snippet.Language = languageRepository.FindBy(x => x.LanguageId == snippet.LanguageId).SingleOrDefault();
                snippet.CodeCategory = categoryRepository.FindBy(x => x.CodeCategoryId == snippet.CodeCategoryId).SingleOrDefault();
            }
            return codeSnippets;
        }


        public static List<CodeSnippet> TakeAsManySnippetsAsWeCan(
            ref bool truncated,
            IRepository<CodeSnippet> codeSnippetRepository,
            Func<CodeSnippet, bool> snippetPred,
            int countOfsnippetsSameAsRequestedGroupSnippets)
        {

            List<CodeSnippet> snippets = new List<CodeSnippet>();

            if (countOfsnippetsSameAsRequestedGroupSnippets > DisplaySnippetsViewModel.MAX_SNIPPETS_TO_DISPLAY)
            {
                snippets = codeSnippetRepository.FindBy(snippetPred).Take(DisplaySnippetsViewModel.MAX_SNIPPETS_TO_DISPLAY).ToList();
                truncated = true;
            }
            else
            {
                snippets = codeSnippetRepository.FindBy(snippetPred).ToList();
                truncated = false;
            }
            return snippets;
        }


       
        public static string Check(double lower, double upper, double toCheck)
        {
            return toCheck > lower && toCheck <= upper ? " checked=\"checked\"" : null;
        }

        public static SnippetRatingModel CreateSnippetRatingModelFromFullSnippet(this CodeSnippet snippet)
        {
            return new SnippetRatingModel(
                   snippet.CodeSnippetId,
                   snippet.Rating,
                   snippet.TotalRatings,
                   Convert.ToDouble(snippet.Rating) / Convert.ToDouble(snippet.TotalRatings)
               );
        }
        
    }
}